package com.itheima.a09threadsafe1;

/**
 * @author zhumq
 * @date 2024/7/23 21:05
 */
public class MyThread extends Thread{
    
    // 这个类所有的对象，共享ticket数据
    static int ticket = 0; // 0 ~ 99
    
    // 锁对象，一定要是唯一的
    static Object obj = new Object();
    @Override
    public void run() {
        while (true) {
            // 同步代码块
            // synchronized (obj) {
            synchronized (MyThread.class) {
                if (ticket < 10){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    ticket++;
                    System.out.println(Thread.currentThread().getName() + "正在卖第[" + ticket+"]张票！！！！");
                }else {
                    break;
                }
            }
        }
    
    }
}
